let [bar, foo] = [1];
console.log(bar)
console.log(foo)

/**
 * 模板字符串
*/
console.log`hello`;
const name = 'lili';
const age = 12;
const addr = 'addr';

function myTag(strings, name, age, addr) {
  console.log(strings, name, age, addr)
  return '12'
}

const result = myTag`${name} is ${age} old, his home is ${addr}`

console.log(result)

//字符串扩展
const _str = 'includes startsWith endsWith';

console.log(_str.includes('starts'))  //true
console.log(_str.startsWith('includes'))  //true
console.log(_str.endsWith('endsWith'))  //true

//对象
const foo2 = 'foo';

const obj = {
  foo2,  // --->es6之前 foo2: foo2,
  [Math.random()]: 15   //计算属性名
}

console.log(obj)

//proxy
const person = {
  name: '小李',
  age: 20
}

// 抽离校验模块
// const personProxy = new Proxy(person, {
//   set(target, key, value, proxy) {
//     if(typeof value !== 'number') {
//       throw Error("Properties in numericDataStore can only be numbers");
//     }
//     return Reflect.set(target, key, value, proxy)
//   }
// })

// personProxy.name = '33'

//Set
function dedupe(array) {
  return Array.from(new Set(array));
  //也可以用这种方式
  return [...new Set(array)];
}

// dedupe([1, 1, 2, 3]) // [1, 2, 3]
console.log(dedupe([1, 1, 2, 3]))

/**
 * 手写Iterator接口
*/

const obj2 = {
  data: ['a', 'b'],
  [Symbol.iterator]: function() {
    let index = 0;
    const self = this;
    return {
      next: function() {
        const result = {
          value: self.data[index],
          done: index === self.data.length
        }
        index++;
        return result;
      }
    }
  }
}

// for(const item of obj2) {
//   console.log(item)
// }

/**
 * 使用Generator 实现iterator方法
*/

const obj3 = {
  data: ['a', 'b'],
  [Symbol.iterator]: function * () {
    const data = this.data;
    for(const item of data) {
      yield item;
    }
  }
}

for(const item of obj3) {
  console.log(item)
}
